using System;
using UnityEngine;
using UnityEditor;
using UnityEngine.EventSystems;

namespace Gj
{
    public class TouchHelper : NodeHelper, IPointerDownHandler, IPointerUpHandler, IDragHandler
    {
        public AudioClip clip;
        public SoundType soundType;
        public TouchItemHelper touchItemHelper;
        public bool click;
        public bool drag;
        private float startTime = 0;
        private bool show;

        private void Update()
        {
            if (startTime == 0) return;
            if (click && !show && Time.time - startTime > SystemInput.clickTime)
            {
                touchItemHelper.SetPosition(transform.position);
                touchItemHelper.Show();
                touchItemHelper.SetData(_data);
                show = true;
            }
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (drag) touchItemHelper.Drag();
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            if (click)
            {
                startTime = Time.time;
                show = false;
            }
            else
            {
                touchItemHelper.SetPosition(transform.position);
                touchItemHelper.Show();
                touchItemHelper.SetData(_data);
                show = true;
            }
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (click && !show)
            {
                Emit(UIEvent.Click);

                if (soundType != SoundType.None)
                    Game.single.PlayEFF(soundType);
                else if (clip != null) Game.single.PlayEFF(clip);
            }
            
            startTime = 0;
            show = false;
            touchItemHelper.Hide();
        }

        protected override void BindData(bool b)
        {
        }
    }
}